import { ref } from 'vue'
import { adapter, request } from '@/request'
import { singleWordCreativeByGet } from '@/core/api/promotion'
import type { Goods } from '@/services/promotion/updatePromotionCreative'
import type { Creative } from '@/core/interfaces/promotion'
import type { PlatformEnum } from '@/core/enums/platform'
import type { ServiceApproveStatusEnum, ServiceStatusEnum } from '@/core/enums/status'
import type { WordTypeEnum } from '@/core/enums/type'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { Time } from '@/core/interfaces/base'

interface Params {
  id: Creative['orderWordId']
}

interface SingleWordData extends Time {
  campaignId: number
  clientType: PlatformEnum
  comboName: string
  content: string
  description1?: string
  description2?: string
  createUserId: number
  creativeId: number
  destinationUrl: string
  pcGoods: Goods[]
  wapGoods: Goods[]
  groupId: number
  id: number
  orderId: number
  orderWordId: number
  serviceApproveStatus: ServiceApproveStatusEnum
  serviceStatus: ServiceStatusEnum
  title: string
  type: WordTypeEnum
  updateUserId: number
  word: string
}

export default function getSingleWordCreative() {
  const service = adapter<RequestWrapper<SingleWordData>>(request)
  const loading = ref(false)
  const response = ref<Partial<SingleWordData>>({})

  const fetch = (params: Params) => {
    const { url, method } = singleWordCreativeByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        res.data.data.pcGoods.forEach((item) => {
          item.pictureURL = `${import.meta.env.VITE_UPLOAD_URL}${item.pictureURL}`
        })

        res.data.data.wapGoods.forEach((item) => {
          item.pictureURL = `${import.meta.env.VITE_UPLOAD_URL}${item.pictureURL}`
        })

        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
